<template>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder=" 请输入用户名"/>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" placeholder=" 请输入邮箱"/>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone" placeholder=" 请输入手机号"/>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="form.address" placeholder=" 请输入地址"/>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="form.age" placeholder=" 请输入年龄"/>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.gender" placeholder=" 请输入性别">
          <el-option label="男" value="Male" />
          <el-option label="女" value="Female" />
        </el-select>
    </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">Create</el-button>
        <el-button>Cancel</el-button>
      </el-form-item>
    </el-form>
  </template>
  
  <script lang="ts" setup>
  import axios from 'axios';
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
  
  // do not use same name with ref
  const form = reactive({
    username: '',
    email: '',
    phone: '',
    address: '',
    age: null,
    gender: ''
  })
  
  const onSubmit = () => {
    console.log('submit!')
    console.log(form);
    axios.post('http://113.45.160.83:8080/insert', form).then(function(resp){
        if(resp.data == true){
            ElMessage({
                message: '用户'+ form.username+ '新增成功',
                type: 'success',
            })        
        }else{
            ElMessage({
                message:'用户'+form.username +'已存在',
                type: 'warning',
            })
        }
    })
  }
  </script>
  